package net.bolue.sysmng.model;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.bolue.console.model.BusiBase;
import net.bolue.sysmng.db.DbOpUtil;
import net.bolue.sysmng.exception.BolueException;
import net.bolue.sysmng.tblbean.TblInfo;
import net.bolue.web.report.RepConstants;

/**
 * 数据表业务类
 * @author Administrator
 *
 */
public class TblInfoModel extends BusiBase{

	private String subSys;
	private List<TblInfo> tblInfoList;
	private HashMap<String,TblInfo> tblInfoMap;
	
	public TblInfoModel() {
		super();
	}

	public TblInfoModel(HttpServletRequest request) {
		super(request);
	}

	/**
	 * 获得所有表
	 * @param subSys	子系统
	 * @return
	 * @throws BolueException
	 */
	public List<TblInfo> getTblInfoList(String subSys) throws BolueException{
		 if (subSys == null){
			 throw new BolueException(5,"参数subSys不能为null!");
		 }
		 DbOpUtil dbOpUtil = new DbOpUtil(RepConstants.SYSMNG_SUBSYS_KEY);
		 String sql = "SELECT * FROM st_tblinfo WHERE SUBSYS=?";
		 Object[] paramters = new Object[]{subSys};
		 List<TblInfo> tblInfoList = dbOpUtil.query(TblInfo.class,sql, paramters);
		 this.tblInfoList = tblInfoList;
		 return tblInfoList;
	}
	
	public Map<String,TblInfo> getTblInfoMap(String subSys) throws BolueException{
		List<TblInfo> tblInfoList = getTblInfoList(subSys);
		 HashMap<String,TblInfo> localHashMap = new HashMap<String,TblInfo>(tblInfoList.size());
		 for(int i=0;i<tblInfoList.size();i++){
			 TblInfo tblInfo = tblInfoList.get(i);
			 localHashMap.put(tblInfo.getTblName(), tblInfo);
		 }
		 return localHashMap;
	}
	
	public TblInfo getTblInfo(String subSys, String tblName) throws BolueException{
		DbOpUtil dbOpUtil = new DbOpUtil(RepConstants.SYSMNG_SUBSYS_KEY);
		String sql = "SELECT * FROM st_tblinfo WHERE SUBSYS=? AND TBLNAME=?";
		Object[] paramters = new Object[]{subSys,tblName};
		TblInfo tblInfo = dbOpUtil.getOne(TblInfo.class, sql, paramters);
		return tblInfo;
	}
}
